package com.yupi.project.service.impl;

import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.ChatCompletionRequest;
import com.zhipu.oapi.service.v4.model.ChatMessage;
import com.zhipu.oapi.service.v4.model.ChatMessageRole;
import com.zhipu.oapi.service.v4.model.ModelApiResponse;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class ZhiPuAIClient {
    private static final String API_KEY = "f86254e487744e96a6726661122eb4a9.73zMy13cgg6k2d3r";
    private static final ClientV4 client = new ClientV4.Builder(API_KEY).build();
    public static String callAI(String prompt) {

        List<ChatMessage> messages = new ArrayList<>();
        ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), prompt);
        messages.add(chatMessage);

        String requestId = String.format("request-%d", System.currentTimeMillis());
        ChatCompletionRequest request = ChatCompletionRequest.builder()
                .model(Constants.ModelChatGLM4) // 替换为你的模型名称
                .stream(Boolean.FALSE)
                .invokeMethod(Constants.invokeMethod)
                .messages(messages)
                .requestId(requestId)
                .build();
        ModelApiResponse response = client.invokeModelApi(request);
        return response.getData().getChoices().get(0).getMessage().getContent().toString();
    }
}
